package com.loong.musicdemo.ui.find.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Singer;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.model.SongList;
import com.loong.musicdemo.ui.find.adapter.SearchPlaylistAdapter;
import com.loong.musicdemo.ui.find.adapter.SingerAdapter;
import com.loong.musicdemo.ui.mine.adapter.SongAdapter;
import com.loong.musicdemo.util.dataDeal.MusicDeal;
import com.loong.musicdemo.util.dataDeal.SearchDeal;

import java.util.ArrayList;
import java.util.List;

public class SearchResultActivity extends AppCompatActivity {
    private Intent intent;
    private Toolbar toolbar;
    private ProgressBar list_progress_bar,singer_progress_bar,song_progress_bar;
    private List<Song> searchSongList = new ArrayList<>();
    private List<SongList> searchPlayLists = new ArrayList<>();
    private List<Singer> searchSingerList = new ArrayList<>();
    private RecyclerView search_result_songs,search_result_singer,search_result_playlist;
    private SongAdapter songAdapter;
    private SingerAdapter singerAdapter;
    private SearchPlaylistAdapter playlistAdapter;
    private List<String> searchSongId = new ArrayList<>();
    private String keywords="";
    private TextView search_none,playlist_tag,singer_tag,song_tag;
    private LinearLayout song_content,singer_content,songList_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initView();
        getIntentData();
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(keywords+"搜索结果");
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        initRecyclerViews();
        new SearchSongTask().execute();
        new SearchSingerTask().execute();
        new SearchPlaylistTask().execute();
    }
    //初始化 控件
    private void initView(){
        search_result_songs = findViewById(R.id.search_result_songs);
        search_result_singer = findViewById(R.id.search_result_singer);
        search_result_playlist = findViewById(R.id.search_result_playlist);
        search_none = findViewById(R.id.search_none);
        song_content = findViewById(R.id.song_content);
        singer_content = findViewById(R.id.singer_content);
        songList_content = findViewById(R.id.songList_content);
        playlist_tag = findViewById(R.id.playlist_tag);
        singer_tag = findViewById(R.id.singer_tag);
        song_tag = findViewById(R.id.song_tag);
        toolbar = findViewById(R.id.toolbar);
        list_progress_bar = findViewById(R.id.list_progress_bar);
        singer_progress_bar = findViewById(R.id.singer_progress_bar);
        song_progress_bar = findViewById(R.id.song_progress_bar);
    }
    //初始化所有的RecyclerView
    private void initRecyclerViews(){
        LinearLayoutManager songManager = new LinearLayoutManager(SearchResultActivity.this);
        songManager.setOrientation(LinearLayoutManager.VERTICAL);
        search_result_songs.setLayoutManager(songManager);
        songAdapter = new SongAdapter(searchSongList);
        search_result_songs.setAdapter(songAdapter);
        LinearLayoutManager singerManager = new LinearLayoutManager(SearchResultActivity.this);
        singerManager.setOrientation(LinearLayoutManager.VERTICAL);
        search_result_singer.setLayoutManager(singerManager);
        singerAdapter = new SingerAdapter(searchSingerList,getApplicationContext());
        search_result_singer.setAdapter(singerAdapter);
        LinearLayoutManager playlistManager = new LinearLayoutManager(SearchResultActivity.this);
        playlistManager .setOrientation(LinearLayoutManager.VERTICAL);
        playlistAdapter = new SearchPlaylistAdapter(searchPlayLists,getApplicationContext());
        search_result_playlist.setLayoutManager(playlistManager);
        search_result_playlist.setAdapter(playlistAdapter);
    }
    //获取搜索关键字
    private void getIntentData(){
        intent = getIntent();
        keywords = intent.getStringExtra("resultName");
    }

    class SearchSongTask extends AsyncTask<Song,Void,List<Song>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(List<Song> songs) {
            super.onPostExecute(songs);
            if(songs.size() == 0){
                song_content.setVisibility(View.GONE);
                if(singer_content.getVisibility()==View.GONE&&songList_content.getVisibility()==View.GONE){
                    search_none.setVisibility(View.VISIBLE);
                }
            }else {
                song_content.setVisibility(View.VISIBLE);
                song_tag.setText("歌曲");
                searchSongList.addAll(songs);
                songAdapter.notifyDataSetChanged();
                song_progress_bar.setVisibility(View.GONE);
            }
        }

        @Override
        protected List<Song> doInBackground(Song... songs) {
            searchSongId = SearchDeal.getSearchSongsId(keywords);
            if(searchSongId.size() != 0){
                return MusicDeal.getSongListByIds(searchSongId);
            }else {
                return new ArrayList<Song>();
            }
        }
    }
    class SearchSingerTask extends AsyncTask<Singer,Void,List<Singer>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<Singer> singers) {
            super.onPostExecute(singers);
            if(singers.size()==0){
                singer_content.setVisibility(View.GONE);
                if(song_content.getVisibility() == View.GONE &&songList_content.getVisibility()==View.GONE){
                    search_none.setVisibility(View.VISIBLE);
                }
            }else {
                singer_content.setVisibility(View.VISIBLE);
                singer_tag.setText("歌手");
                searchSingerList.addAll(singers);
                singerAdapter.notifyDataSetChanged();
                singer_progress_bar.setVisibility(View.GONE);
            }
        }

        @Override
        protected List<Singer> doInBackground(Singer... singers) {
            return SearchDeal.getSearchSingers(keywords);
        }
    }
    class SearchPlaylistTask extends AsyncTask<SongList,Void,List<SongList>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<SongList> songLists) {
            super.onPostExecute(songLists);
            if(songLists.size() == 0){
                songList_content.setVisibility(View.GONE);
                if(song_content.getVisibility() == View.GONE&&singer_content.getVisibility()==View.GONE){
                    search_none.setVisibility(View.VISIBLE);
                }
            }else {
                playlist_tag.setText("歌单");
                songList_content.setVisibility(View.VISIBLE);
                searchPlayLists.addAll(songLists);
                playlistAdapter.notifyDataSetChanged();
                list_progress_bar.setVisibility(View.GONE);
            }

        }

        @Override
        protected List<SongList> doInBackground(SongList... songLists) {
            return SearchDeal.getSearchPlayLists(keywords);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            this.finish();
        return super.onOptionsItemSelected(item);
    }
}
